const router = require('express').Router()
const { wrap } = require('../tool/index')
const { Follow_log, Agree_log, Collection_log } = require('../config/sql')

//粉丝
router.get('/fans', wrap(async (req, res) => {
   const { user: { account: bid } } = req
   const fans = await Follow_log.findAll({
      attributes: ['uid', 'bid'],
      where: {
         bid
      }
   })
   res.send({
      code: 200,
      fans,
      count: fans.length
   })
}))

// 我的关注
router.get('/follow/me', wrap(async (req, res) => {
   const { user: { account: uid } } = req
   const collection = await Follow_log.findAll({
      attributes: ['uid', 'bid'],
      where: {
         uid
      }
   })
   res.send({
      code: 200,
      collection
   })
}))

// 关注
router.get('/follow', wrap(async (req, res) => {
   const { user: { account: uid }, query: { id: bid } } = req
   const [log] = await Follow_log.findAll({
      where: {
         uid,
         bid
      }
   })
   if (log) return res.send({
      code: 325,
      msg: '已关注'
   })
   await Follow_log.create({
      uid,
      bid
   })
   res.send({
      code: 200
   })
}))

// 取消关注
router.get('/follow/cancel', wrap(async (req, res) => {
   const { user: { account: uid }, query: { id: bid } } = req
   const [log] = await Follow_log.findAll({
      where: {
         uid,
         bid
      }
   })
   if (!log) return res.send({
      code: 325,
      msg: '未关注'
   })
   await log.destroy()
   res.send({
      code: 200
   })
}))

// 我的点赞
router.get('/agree/me', wrap(async (req, res) => {
   const { user: { account: uid } } = req
   const agree = await Agree_log.findAll({
      attributes: ['uid', 'aid'],
      where: {
         uid
      }
   })
   res.send({
      code: 200,
      agree
   })
}))

// 点赞文章
router.get('/agree', wrap(async (req, res) => {
   const { user: { account: uid }, query: { id: aid } } = req
   const [log] = await Agree_log.findAll({
      where: {
         uid,
         aid
      }
   })
   if (log) return res.send({
      code: 325,
      msg: '已点赞'
   })
   await Agree_log.create({
      uid,
      aid
   })
   res.send({
      code: 200
   })
}))

// 取消点赞
router.get('/agree/cancel', wrap(async (req, res) => {
   const { user: { account: uid }, query: { id: aid } } = req
   const [log] = await Agree_log.findAll({
      where: {
         uid,
         aid
      }
   })
   if (!log) return res.send({
      code: 325,
      msg: '未点赞'
   })
   await log.destroy()
   res.send({
      code: 200
   })
}))

// 我的收藏
router.get('/collection/me', wrap(async (req, res) => {
   const { user: { account: uid } } = req
   const collection = await Collection_log.findAll({
      attributes: ['uid', 'aid'],
      where: {
         uid
      }
   })
   res.send({
      code: 200,
      collection
   })
}))

// 收藏文章
router.get('/collection', wrap(async (req, res) => {
   const { user: { account: uid }, query: { id: aid } } = req
   const [log] = await Collection_log.findAll({
      where: {
         uid,
         aid
      }
   })
   if (log) return res.send({
      code: 325,
      msg: '已收藏'
   })
   await Collection_log.create({
      uid,
      aid
   })
   res.send({
      code: 200
   })
}))

// 取消收藏
router.get('/collection/cancel', wrap(async (req, res) => {
   const { user: { account: uid }, query: { id: aid } } = req
   const [log] = await Collection_log.findAll({
      where: {
         uid,
         aid
      }
   })
   if (!log) return res.send({
      code: 325,
      msg: '未收藏'
   })
   await log.destroy()
   res.send({
      code: 200
   })
}))
module.exports = router